Ask a Question
Send an email to me.
 
Java Question and Answer Forums
JavaRanch Big Moose Saloon
Marcus Green's Discussion Forum
java.sun.com Forums, Chat and User Groups
Artima Java Answers Forum
 
Other Resources
Java Language Specification
Java Virtual Machine Specification
Java 2 Platform, Standard Edition, v 1.4.0 API Specification
 
Tutorials
Learning the Java Language
Operator Precedence Chart, Expressions, Statements, Blocks
Programming with Assertions
 
Answers: Certified Java Programmer Mock Exam
No.AnswerRemark
1f  Compiler Error  The loop variable, h, is local to the "for" statement. The print statement causes a compiler error because it refers to the local variable h. Always check for compiler errors due to the use of out-of-scope variables.  
2d  Prints: ABCBCC  There are no break statements for the first case falls through to the second case and the second case falls through to the third. There is no default case so no case statement is processed when the switch expression is 3.  
3b  Prints: 1,0,1,0,0,1  No exception is thrown so a is incremented. Both of the finally blocks are then executed.  
4f  Compiler Error  A compile-time error is generated because the second catch clause attempts to catch an exception that is never thrown in the try block.  
5a  e  f  Compiler error at line 2.  Compiler error at line 6.  Compiler error at line 7.  All of the exceptions in this question are a subclass of Exception and three are a subclass of RuntimeException. Any exception that is a subclass of RuntimeException should not be caught. Instead, it should be allowed to bubble to the top of the program where the runtime system will display a stack trace. RuntimeExceptions are a result of program bugs. A RuntimeException should not be listed in the throws clause of a program. Methods m1, m5, and m6 throw subclasses of Exception that are not subclasses of RuntimeException. Any Exception that is a direct subclass of Exception must either be caught or must be declared in the throws clause of the method.  
6b  c  e  Compiler error at line 3.  Compiler error at line 4.  Compiler error at line 6.  All of the exceptions in this question are a subclass of Exception and three are a subclass of RuntimeException. Any exception that is a subclass of RuntimeException should not be caught. Instead, it should be allowed to bubble to the top of the program where the runtime system will display a stack trace. RuntimeExceptions are a result of program bugs. A RuntimeException should not be listed in the throws clause of a program. Methods m2, m3, and m5 throw subclasses of Exception that are not subclasses of RuntimeException. Any Exception that is a direct subclass of Exception must either be caught or must be declared in the throws clause of the method.  
7a  Prints: 0,0,0,1,1  The first catch block is able to catch a Level3Exception or any subclass of Level3Exception. The second catch block is able to catch a Level2Exception or any subclass of Level2Exception. The third catch block is the only one that is able to catch a Level1Exception. The finally block is also executed.  
8b  Prints: 0,0,1,1,0,1  The nested catch block is able to catch a Level2Exception or any subclass of it. The Level1Exception is not a subclass of Level2Exception so it is caught by the second of the two outer catch blocks. Both finally blocks are executed.  
9a  b  c  d  e  f  Compiler error at line 2.  Compiler error at line 3.  Compiler error at line 4.  Compiler error at line 5.  Compiler error at line 6.  Compiler error at line 7.  Methods m1(), m2(), m3(), m4(), m5(), and m6() throw subclasses of Exception that are not subclasses of RuntimeException. Any Exception that is a direct subclass of Exception must either be caught or must be declared in the throws clause of the method.  
10b  Prints: false,true  Error is a direct subclass of Throwable. RuntimeException is a direct subclass of Exception.  
11c  Prints: v w x x y z z  Cases one and three have no break statement, so the next case is also executed and x and z are printed twice.  
12d  Prints: true,true  Both Error and Exception are subclasses of Throwable.  
13f  Compiler error  The Java Language Specification states that "Every case constant expression associated with a switch statement must be assignable to the type of the switch expression." The constant expression 1000 is beyond the range of type byte.  
14c  Prints: 1433  The initial value of X is -5. On the first pass through the loop the default case of the switch statement is invoked and 2 is added to the value of x. The new value is -3. On the second pass, the default case of the switch statement is invoked again and two is added to the value of x. The new value is negative 1. On the third pass 2 is added again and the new value of x is positive 1. After that, the value of x is printed on each pass through the loop. On the last two passes through the loop the value of x is 3.  
15c  Prints: i1k1i2j1i3j2  A while loop is nested inside of a do loop. The while loop iterates twice during the first iteration of the do loop. The body of the while loop does not excute during the final two iterations of the do loop.  
16f  Prints: 8,4  While j is greater than or equal to seven the variable, i, is incremented twice with each pass through the loop. When j is less than seven the continue statement causes the first increment of variable i to be skipped. The continue statement causes control to go directly to the loop expression that causes i to be incremented after checking to see that i is less than seven. When i reaches eight the loop expression is false and the loop is terminated.  
17h  Prints: 121212  Although a program such as this will probably never be found in the real world, something similar is likely to be found on the real exam. This trick question has a for-loop nested inside of a do-loop. The body of each loop is a single statement rather than a block. The best way to understand a program involving the operation of nested loops is to modify the print statement so that it prints all of the variables involved in the loop. The printed results will provide the best possible explanation. The values of i, j and k for each iteration are as follows: (1,0)(2,0)(1,1)(2,1)(1,2)(2,2).  
18c  Prints: 6,6  The variable, i, is incremented twice with each pass through the loop. The variable, j, is decremented once with each pass. The loop terminates when i reaches six.  
19f  Prints: 1112  Although a program such as this will probably never be found in the real world, something similar is likely to be found on the real exam. This trick question has a do-loop nested inside of a for-loop. The body of each loop is a single statement rather than a block. The best way to understand a program involving the operation of nested loops is to modify the print statement so that it prints all of the variables involved in the loop. The printed results will provide the best possible explanation. The values of i, j and k for each iteration are as follows: (1,0)(1,1)(1,2)(2,3).  

Copyright © 2002, Dan Chisholm
All rights reserved.
1